import java.sql.Connection;
import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;

public class Main {
	public static Main singleton;

	Connection connection;

	Game game;

	Display display;
	Shell shell;

	Label lbStatus;

	Main(){
		display = new Display();
		singleton = this;
	}

	boolean setup() {
		Shell setup = new Shell(display, SWT.TITLE);
		setup.setText("康威生命游戏");
		setup.setLayout(new GridLayout(2, false));
		GridData gdHSpan2 = new GridData(SWT.CENTER, SWT.CENTER, true, false, 2, 1);

			Label lbHeadline = new Label(setup, SWT.NONE);
			lbHeadline.setLayoutData(gdHSpan2);
			lbHeadline.setText("设置");

			Label lbWidth = new Label(setup, SWT.NONE);
			lbWidth.setText("地图宽度：");

			Spinner spWidth = new Spinner(setup, SWT.BORDER);
			spWidth.setSelection(20);
			spWidth.setMinimum(1);

			Label lbHeight = new Label(setup, SWT.NONE);
			lbHeight.setText("地图高度：");

			Spinner spHeight = new Spinner(setup, SWT.BORDER);
			spHeight.setSelection(20);
			spHeight.setMinimum(1);
		
//			Button btPeriodicBorder = new Button(setup, SWT.CHECK);
//			btPeriodicBorder.setLayoutData(gdHSpan2);
//			btPeriodicBorder.setText("周期边界");

			Button btConfirm = new Button(setup, SWT.PUSH);
			btConfirm.setText("确定");
			btConfirm.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					game = new Game(shell, 
									SWT.BORDER, 
									spWidth.getSelection(), spHeight.getSelection(), 
									false);
//									btPeriodicBorder.getSelection());
					game.setLayoutData(new RowData(1000, 500));
					game.setFocus();
					game.addListener(SWT.SetData, new Listener() {
						public void handleEvent(Event e) {
							lbStatus.setText((boolean)e.data ? "已暂停" : "运行中");
						}
					});

					setup.dispose();

					shell.pack();
					shell.open();
					game.gameRedraw();
				}
			});

			Button btCancel = new Button(setup, SWT.PUSH);
			btCancel.setText("取消");
			btCancel.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					setup.dispose();
				}
			});

			setup.pack();
			setup.open();

			while(!setup.isDisposed()) {
				if(!display.readAndDispatch()) {
					display.sleep();
				}
			}

			return game != null;
	}

	void init() {
		shell = new Shell(display, SWT.TITLE | SWT.MIN | SWT.CLOSE);
		shell.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				display.dispose();
			}
		});
		shell.setText("康威生命游戏");
		RowLayout shLayout = new RowLayout();
		shLayout.type = SWT.VERTICAL;
		shell.setLayout(shLayout);

		Composite composite1 = new Composite(shell, SWT.NONE);
		RowLayout cmp1Layout = new RowLayout();
		cmp1Layout.type = SWT.HORIZONTAL;
		cmp1Layout.center = true;
		composite1.setLayout(cmp1Layout);

			Label lbInterval = new Label(composite1, SWT.NONE);
			lbInterval.setText("帧间隔：");

			Spinner spInterval = new Spinner(composite1, SWT.BORDER);
			spInterval.setSelection(100);
			spInterval.setMinimum(5);
			spInterval.setMaximum(Integer.MAX_VALUE);
			spInterval.setIncrement(100);
			spInterval.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					game.setInterval(spInterval.getSelection());
				}
			});

			Label lbMs = new Label(composite1, SWT.NONE);
			lbMs.setText("ms");

			Label lbSeperator = new Label(composite1, SWT.SEPARATOR | SWT.VERTICAL);
			lbSeperator.setLayoutData(new RowData(20, 30));

			lbStatus = new Label(composite1, SWT.NONE);
			lbStatus.setText("已暂停");

			Label lbSeperator2 = new Label(composite1, SWT.SEPARATOR | SWT.VERTICAL);
			lbSeperator2.setLayoutData(new RowData(20, 30));

			Button btDatabase = new Button(composite1, SWT.PUSH);
			btDatabase.setText("数据库");
			btDatabase.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					try {
						DBMenu menu = new DBMenu(display, connection == null || connection.isClosed() ? null : connection);
					} catch (Exception e) {
						MessageBox box = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
						box.setText("严重错误");
						box.setMessage("实例化JDBC驱动器时出错：\n" + e.toString());
					}
				}
			});

			Label lbSeperator3 = new Label(composite1, SWT.SEPARATOR | SWT.VERTICAL);
			lbSeperator3.setLayoutData(new RowData(20, 30));

			Label lbInstruction = new Label(composite1, SWT.NONE);
			lbInstruction.setText("空格：开始/暂停        ALT+鼠标拖动：移动视野        左键/右键 单击/拖动：编辑        滚轮：缩放");

		shell.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent event) {
				try {
					if(Main.this.connection != null)
						Main.this.connection.close();
				} catch (SQLException e) { 
					e.printStackTrace();
				}
			}
		});

		composite1.pack();
	}


	void open() {
		shell.pack();
		shell.open();
	}

	public static void main(String [] args) {

		Main main = new Main();
		main.init();
		if(!main.setup())
			return;

		while(!main.display.isDisposed()) {
			if(!main.display.readAndDispatch()) {
				main.display.sleep();
			}
		}
	}

	public Game getGame() {
		return game;
	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}
}
